package com.ccf.business.knowledge.model.vo;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.ccf.business.knowledge.model.LabelData;
import com.ccf.business.knowledge.model.LabelTask;
import com.ccf.business.knowledge.service.ILabelDataService;
import com.common.link.model.EntityVO;
import com.common.link.model.RelationAndEntityVO;
import com.common.model.annotation.RealField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 描述：
 * 任务实体
 * @author xianggj
 * @Date 2022/2/18 17:57
 **/
@Data
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value="任务详情", description="标注任务详情")
public class LabelTaskVO extends LabelTask {

    @ApiModelProperty(value = "文件id列表")
    private String[] fileList;

    @ApiModelProperty(value = "标注详情")
    private List<LabelData> fileDatas = Lists.newArrayList();

    @ApiModelProperty(value = "标注统计信息")
    private List<LabelStatisticalVO> statistical = Lists.newArrayList();

    @ApiModelProperty(value = "用户id列表")
    private String[] userList;

    @ApiModelProperty(value = "用户名称字符串, 用于查看详情回显")
    private String userListStr;

    @ApiModelProperty(value = "实体id列表")
    private String[] entityList;

    @ApiModelProperty(value = "关系列表")
    private RelationTypeVO[] relationList;

    @ApiModelProperty(value = "实体列表")
    private List<EntityVO>  entityDatas;

    @ApiModelProperty(value = "关系列表")
    private List<RelationAndEntityVO>  relationDatas;

    @ApiModelProperty(value = "文件数量")
    //先排除错误
    @RealField("name")
    private Integer fileNum;

    @ApiModelProperty(value = "未标注数量")
    private Long notLabelNum;

    @ApiModelProperty(value = "标注数量")
    private Long labelNum;

    @ApiModelProperty(value = "审核数量")
    private Long auditNum;

    @ApiModelProperty(value = "标注进度")
    private String labelRate;

    @ApiModelProperty(value = "审核进度")
    private String auditRate;

    @ApiModelProperty(value = "标注类型字符串")
    @RealField("label_type")
    private String labelTypeStr;

    @ApiModelProperty(value = "标注方式字符串")
    @RealField("label_way")
    private String labelWayStr;

    @ApiModelProperty(value = "状态字符串")
    private String statusStr;

    public String getLabelTypeStr() {
        if (getLabelType() == null){
            return null;
        }
        switch (getLabelType()) {
            case ENTITY:
                return "实体标注";
            case RELATION:
                return "关系标注";
            default:
                return null;
        }
    }

    public String getLabelWayStr() {
        if (getLabelWay() == null){
            return null;
        }
        switch (getLabelWay()) {
            case AVG:
                return "均分标注";
            case COMMON:
                return "共同标注";
            default:
                return null;
        }
    }

    public String getStatusStr() {
        if (getStatus() == null){
            return null;
        }
        switch (getStatus()) {
            case WAIT:
                return "待下发";
            case TAGGING:
                return "标注中";
            case AUDIT:
                return "审核中";
            case COMPLETE:
                return "已完成";
            default:
                return null;
        }
    }

    public LabelTaskVO(LabelTask labelTask, ILabelDataService labelDataService) {
        this(labelTask, labelDataService, null);
    }

    public LabelTaskVO(LabelTask labelTask, ILabelDataService labelDataService, String userId) {
        //均分标注 过滤数据
        BeanUtil.copyProperties(labelTask, this);
        List<LabelData> list = labelDataService.list(Wrappers.<LabelData>query()
                .lambda()
                .eq(LabelData::getTaskId, labelTask.getId())
                .eq(userId != null && LabelTask.AVG.equalsIgnoreCase(labelTask.getLabelWay()), LabelData::getTager, userId)
        );
        this.fileNum = list.size();
        this.notLabelNum = list.stream()
                .filter(e->e.getStatus() != null && e.getStatus() <= LabelData.TAGGING)
                .count();
        this.labelNum = list.stream()
                .filter(e->e.getStatus() != null && e.getStatus() > LabelData.TAGGING)
                .count();
        this.auditNum = list.stream()
                .filter(e->e.getStatus() != null && e.getStatus() > LabelData.WAITING_AUDIT)
                .count();
        this.labelRate = this.labelNum + "/" + list.size();
        this.auditRate = this.auditNum + "/" + list.size();

    }
}
